﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog;
using NLog.Config;
using NLog.Win32.Targets;
using NLog.Targets;

namespace MyAlbum.Logging
{
    public class NLogger:ILogger
    {
        #region ILogger Members
        private static Logger logger;
        static NLogger()
        {
            // Step 1. Create configuration object 

            LoggingConfiguration config = new LoggingConfiguration();

            // Step 2. Create targets and add them to the configuration 

            ColoredConsoleTarget consoleTarget = new ColoredConsoleTarget();
            config.AddTarget("console", consoleTarget);

            FileTarget fileTarget = new FileTarget();
            config.AddTarget("file", fileTarget);

            // Step 3. Set target properties 

            consoleTarget.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
            fileTarget.FileName = "${basedir}/file.txt";
            fileTarget.Layout = "${message}";

            // Step 4. Define rules 

            LoggingRule rule1 = new LoggingRule("*", LogLevel.Debug, consoleTarget);
            config.LoggingRules.Add(rule1);

            LoggingRule rule2 = new LoggingRule("*", LogLevel.Debug, fileTarget);
            config.LoggingRules.Add(rule2);

            // Step 5. Activate the configuration 

            LogManager.Configuration = config;

            // Example usage 

            logger = LogManager.GetLogger("Example");
            logger.Trace("trace log message");
            logger.Debug("debug log message");
            logger.Info("info log message");
            logger.Warn("warn log message");
            logger.Error("error log message");
            logger.Fatal("fatal log message");
        }
        public override void LogError(Exception e1)
        {
            LogError(e1, null);
        }

        public override void LogError(string message)
        {
            LogError(null, message);
        }

        public override void LogError(Exception e1, string message)
        {
            logger.LogException(LogLevel.Error, message, e1);
        }

        public override void Trace(object data)
        {
            logger.Trace(data);
        }

        public override void Info(string message)
        {
            logger.Info(message);
        }


        public override void Warning(string message)
        {
            logger.Warn(message);
        }

        #endregion
    }
}
